package com.ispeed.fluttershensu;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.VpnService;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

/**
 * @author timmy
 */
@SuppressWarnings("SpellCheckingInspection")
public class MainActivity extends FlutterActivity implements MainConstant.ISpeedView {

    private static final String CHANNEL_NAME = "com.aisu.ispeed/android";
    private static final String METHOD_ISPEED_STATE = "getISpeedState";
    private static final String METHOD_START_ISPEED = "startISpeed";
    private static final String METHOD_STOP_ISPEED = "stopISpeed";

    private static final String ARGUMENTS_STOP_ISPEED = "stop_ispeed";
    private static final String ARGUMENTS_ISPEED_STATE = "get_ispeed_state";

    private static final int VPN_REQUEST_CODE = 0x0F;

    MainConstant.ISpeedPresenter mISpeedPresenter;
    private MethodChannel.Result mChannelResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);

        mISpeedPresenter = new MainActivityPresenter(this);
        initFlutter();
    }

    private void initFlutter() {
        new MethodChannel(getFlutterView(), CHANNEL_NAME).setMethodCallHandler(
                new MethodChannel.MethodCallHandler() {
                    @Override
                    public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
                        mChannelResult = result;
                        String arguments = (String) methodCall.arguments;

                        switch (methodCall.method) {
                            case METHOD_START_ISPEED:
                                Toast.makeText(MainActivity.this, "arguments: " + arguments, Toast.LENGTH_SHORT).show();
                                startISpeed(arguments);
                                Log.i("gzw", "arguments: " + arguments);
                                return;
                            case METHOD_STOP_ISPEED:
                                if (ARGUMENTS_STOP_ISPEED.equals(arguments)) {
                                    stopISpeed();
                                    result.success(false);
                                    return;
                                }
                                break;
                            case METHOD_ISPEED_STATE:
                                // 获取加速状态
                                if (ARGUMENTS_ISPEED_STATE.equals(arguments)) {
                                    result.success(LocalVPNService.IsRunning);
                                    return;
                                }
                                break;
                            default:
                                result.notImplemented();
                                return;
                        }

                        result.error("UNAVAILABLE", "Battery level not available.", null);
                    }
                }
        );
    }


    private void startISpeed(String arguments) {
        if (!LocalVPNService.IsRunning) {
            boolean speedState = mISpeedPresenter.getSpeedState();
            if (!TextUtils.isEmpty(arguments)) {
                SharedPreferences sharedPreferences = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putString("arguments", arguments);
                edit.apply();
            }

            Intent vpnIntent = VpnService.prepare(this);
            if (vpnIntent != null) {
                startActivityForResult(vpnIntent, VPN_REQUEST_CODE);
            } else {
                onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
            }
        }
    }

    private void stopISpeed() {
        boolean speedState = mISpeedPresenter.getSpeedState();
        if (speedState) {
            mISpeedPresenter.startISpeed();
        }
        if (LocalVPNService.IsRunning) {
            LocalVPNService.IsRunning = false;
            LocalVPNService.Instance.disconnectVPN();
            stopService(new Intent(this, LocalVPNService.class));
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == VPN_REQUEST_CODE && resultCode == RESULT_OK) {
            startService(new Intent(this, LocalVPNService.class));
            mISpeedPresenter.startISpeed();
            if (mChannelResult != null) {
                mChannelResult.success(true);
            }
        } else {
            mChannelResult.success(false);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mChannelResult = null;
    }
}
